package com.aw.baselib.util

import android.app.Activity
import android.view.View
import android.widget.PopupWindow
import com.aw.baselib.bean.CommonListBean
import com.aw.baselib.widgets.AwCommonListPopupWindow
import com.aw.baselib.widgets.AwCommonListWithTitlePopupWindow
import com.aw.baselib.widgets.AwCommonTopListPopupWindow

/**
 * Created by hzw
 */
object AwPopupwindowUtil {
    fun showCommonListWithTitlePopupWindow(
        mActivity: Activity?,
        title: String?,
        list: List<out CommonListBean>,
        locationView: View?,
        onItemClickListener: AwCommonListWithTitlePopupWindow.OnItemClickListener?
    ) {
        val commonListPopupWindow = AwCommonListWithTitlePopupWindow(
            mActivity!!, title, null
        )
        commonListPopupWindow.setListData(list)
        commonListPopupWindow.setOnItemClickListener(onItemClickListener)
        commonListPopupWindow.showAsLocationBottom(locationView)
    }

    fun showCommonListPopupWindow(
        mActivity: Activity?,
        list: List<out CommonListBean>,
        locationView: View?,
        onItemClickListener: AwCommonListPopupWindow.OnItemClickListener?
    ) {
        val commonListPopupWindow = AwCommonListPopupWindow(
            mActivity!!
        )
        commonListPopupWindow.setListData(list)
        commonListPopupWindow.setOnItemClickListener(onItemClickListener)
        commonListPopupWindow.showAsLocationBottom(locationView)
    }

    fun showCommonTopListPopupWindowWithParentAndDismissNoAlpha(
        mActivity: Activity?,
        list: MutableList<Any>?,
        locationView: View,
        dismissListener: PopupWindow.OnDismissListener?,
        onItemClickListener: AwCommonTopListPopupWindow.OnItemClickListener?
    ) {
        val commonListPopupWindow = AwCommonTopListPopupWindow(
            mActivity!!, dismissListener
        )
        commonListPopupWindow.setListData(list)
        commonListPopupWindow.setOnItemClickListener(onItemClickListener)
        commonListPopupWindow.showAsLocationNoAlpha(locationView)
    }

    /**
     * 计算出来的位置，y方向就在anchorView的上面和下面对齐显示，x方向就是与屏幕右边对齐显示
     * 如果anchorView的位置有变化，就可以适当自己额外加入偏移来修正
     * @param anchorView  呼出window的view
     * @param contentView   window的内容布局
     * @return window显示的左上角的xOff,yOff坐标
     */
    fun calculatePopWindowPos(anchorView: View, contentView: View): IntArray {
        val windowPos = IntArray(2)
        val anchorLoc = IntArray(2)
        //获取锚点View在屏幕上的左上角坐标位置
        anchorView.getLocationOnScreen(anchorLoc)
        val anchorHeight = anchorView.height
        // 获取屏幕的高宽
        val screenHeight = AwScreenUtils.getScreenHeight(anchorView.context)
        val screenWidth = AwScreenUtils.getScreenWidth(anchorView.context)
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
        // 计算contentView的高宽
        val windowHeight = contentView.measuredHeight
        val windowWidth = contentView.measuredWidth
        // 判断需要向上弹出还是向下弹出显示
        val isNeedShowUp = screenHeight - anchorLoc[1] - anchorHeight < windowHeight
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - windowWidth
            windowPos[1] = anchorLoc[1] - windowHeight
        } else {
            windowPos[0] = screenWidth - windowWidth
            windowPos[1] = anchorLoc[1] + anchorHeight
        }
        return windowPos
    }
}